{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Even Odd Subarray With Threshold"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sliding-window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #滑动窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: longestAlternatingSubarray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最长奇偶子数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>threshold</code> 。</p>\n",
    "\n",
    "<p>请你从 <code>nums</code> 的子数组中找出以下标 <code>l</code> 开头、下标 <code>r</code> 结尾 <code>(0 &lt;= l &lt;= r &lt; nums.length)</code> 且满足以下条件的 <strong>最长子数组</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>nums[l] % 2 == 0</code></li>\n",
    "\t<li>对于范围&nbsp;<code>[l, r - 1]</code> 内的所有下标 <code>i</code> ，<code>nums[i] % 2 != nums[i + 1] % 2</code></li>\n",
    "\t<li>对于范围&nbsp;<code>[l, r]</code> 内的所有下标 <code>i</code> ，<code>nums[i] &lt;= threshold</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>以整数形式返回满足题目要求的最长子数组的长度。</p>\n",
    "\n",
    "<p><strong>注意：子数组</strong> 是数组中的一个连续非空元素序列。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [3,2,5,4], threshold = 5\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>在这个示例中，我们选择从 l = 1 开始、到 r = 3 结束的子数组 =&gt; [2,5,4] ，满足上述条件。\n",
    "因此，答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2], threshold = 2\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>\n",
    "在这个示例中，我们选择从 l = 1 开始、到 r = 1 结束的子数组 =&gt; [2] 。\n",
    "该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [2,3,4,5], threshold = 4\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>\n",
    "在这个示例中，我们选择从 l = 0 开始、到 r = 2 结束的子数组 =&gt; [2,3,4] 。 \n",
    "该子数组满足上述全部条件。\n",
    "因此，答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100 </code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 100 </code></li>\n",
    "\t<li><code>1 &lt;= threshold &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-even-odd-subarray-with-threshold](https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-even-odd-subarray-with-threshold](https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[3,2,5,4]\\n5', '[1,2]\\n2', '[2,3,4,5]\\n4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            count = 0\n",
    "            if nums[i] % 2 == 0 and nums[i] <= threshold:\n",
    "                count = 1\n",
    "                j = i + 1\n",
    "                while j < n and nums[j] % 2 != nums[j-1] % 2 and nums[j] <= threshold:\n",
    "                    count += 1\n",
    "                    j += 1\n",
    "            ans = max(ans,count)\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, acc, state, parity = 0, 0, False, 0\n",
    "        for index in range(len(nums)):\n",
    "            if not state:\n",
    "                if nums[index] % 2 == parity and nums[index] <= threshold:\n",
    "                    state = True\n",
    "                    acc += 1\n",
    "                    parity = not parity\n",
    "                    res = max(res, acc)\n",
    "            else:\n",
    "                if nums[index] % 2 == parity and nums[index] <= threshold:\n",
    "                    acc += 1\n",
    "                    parity = not parity\n",
    "                    res = max(res, acc)\n",
    "                else:\n",
    "                    if nums[index] % 2 == 0 and nums[index] <= threshold:\n",
    "                        state, acc, parity = True, 1, 1\n",
    "                        res = max(res, acc)\n",
    "                    else:\n",
    "                        acc, state, parity = 0, False, 0\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        tmp = 0\n",
    "        out = 0\n",
    "        for num in nums:\n",
    "            if tmp%2 == num%2 and num<=threshold:\n",
    "                tmp += 1\n",
    "            else:\n",
    "                out = max(out, tmp)\n",
    "                tmp = 0\n",
    "                if tmp%2 == num%2 and num<=threshold:\n",
    "                    tmp += 1\n",
    "            # print(num,tmp,out)\n",
    "        out = max(out, tmp)\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = flag = cnt = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] <= threshold and nums[i] & 1 == flag:\n",
    "                cnt += 1\n",
    "                flag ^= 1\n",
    "            else:\n",
    "                ans = max(ans, cnt)\n",
    "                cnt = flag = 0\n",
    "                # 处理两个偶数的情况\n",
    "                if nums[i] <= threshold and nums[i] & 1 == 0:\n",
    "                    cnt = flag = 1\n",
    "                    flag = 1\n",
    "\n",
    "        return max(ans, cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = m = mx = 0\n",
    "        for n in nums:\n",
    "            if n % 2 == m and n <= threshold:\n",
    "                mx += 1\n",
    "                m ^= 1\n",
    "            elif n % 2 == 0 and n <= threshold:\n",
    "                ans = max(ans, mx)\n",
    "                mx = m = 1\n",
    "            else:\n",
    "                ans = max(ans, mx)\n",
    "                mx = m = 0\n",
    "        return max(ans, mx)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        #slide window\n",
    "      \n",
    "        n=len(nums)\n",
    "        start=-1\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            if nums[i]<=threshold:\n",
    "                if start==-1:\n",
    "                    if nums[i]%2==0:\n",
    "                        start=i \n",
    "                else:\n",
    "                    if nums[i]%2==nums[i-1]%2:\n",
    "                        ans=max(ans,i-start)\n",
    "                        if nums[i]%2==0:\n",
    "                            start=i \n",
    "                        else:\n",
    "                            start=-1\n",
    "            else:\n",
    "                if start!=-1:\n",
    "                    ans=max(ans,i-start)\n",
    "                    start=-1\n",
    "        if start!=-1:\n",
    "            ans=max(ans,n-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        l, r = 0, 0\n",
    "        while l<n:\n",
    "            if nums[l]%2==0 and nums[l] <= threshold:\n",
    "                r = l+1\n",
    "                while r<n and nums[r]<=threshold and nums[r]%2!=nums[r-1]%2:\n",
    "                    r += 1\n",
    "                if r<n:\n",
    "                    ans = max(ans, r-l)\n",
    "                    if nums[r] > threshold:\n",
    "                        l = r+1\n",
    "                    else:\n",
    "                        l = r\n",
    "                else: # r>=n\n",
    "                    ans = max(ans, r-l)\n",
    "                    break\n",
    "            else:\n",
    "                l += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        l = 0\n",
    "        while l < len(nums):\n",
    "            if nums[l] % 2 != 0 or nums[l] > threshold:\n",
    "                l += 1\n",
    "                continue\n",
    "            r = l\n",
    "            is_odd = 1\n",
    "            while r + 1 < len(nums) and nums[r + 1] % 2 == is_odd and nums[r + 1] <= threshold:\n",
    "                r += 1\n",
    "                is_odd = 1 - is_odd\n",
    "            res = max(res, r - l + 1)\n",
    "            l = r + 1\n",
    "        return res            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        l = -1\n",
    "        for r in range(len(nums)):\n",
    "            if nums[r] > threshold:\n",
    "                l = -1\n",
    "            elif l < 0:\n",
    "                if nums[r] % 2 == 0:\n",
    "                    l = r\n",
    "                    ans = max(ans,1)\n",
    "            else:\n",
    "                if nums[r] % 2 != nums[r-1] % 2:\n",
    "                    ans = max(ans,r-l+1)\n",
    "                else:\n",
    "                    l = r if nums[r] % 2 == 0 else -1\n",
    "                    ans = max(ans,1)\n",
    "        return ans  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, dp = 0, 0\n",
    "        for l in range(len(nums) - 1, -1, -1):\n",
    "            if nums[l] > threshold:\n",
    "                dp = 0\n",
    "            elif l == len(nums) - 1 or nums[l] % 2 != nums[l+1] % 2:\n",
    "                dp += 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            res = dp if nums[l] % 2 == 0 and dp > res else res\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        m = 0\n",
    "        res = 0\n",
    "        while m < n:\n",
    "            if nums[m] % 2 == 0 and nums[m] <= threshold:\n",
    "                # print(m)\n",
    "                l = r = m\n",
    "                while r < n - 1:\n",
    "                    if nums[r] % 2 != nums[r + 1] % 2 and nums[r + 1] <= threshold:\n",
    "                        r += 1\n",
    "                    else:\n",
    "                        res = max(res, r - l + 1)\n",
    "                        break\n",
    "                res = max(res, r - l + 1)      \n",
    "            m += 1\n",
    "        return res\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        i, ans = 0, 0\n",
    "        while i < n:\n",
    "            if i < n and nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            if nums[i] % 2 != 0 or nums[i] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            j = i + 1\n",
    "            while j < len(nums):\n",
    "                if nums[j - 1] % 2 == nums[j] % 2 or nums[j] > threshold:\n",
    "                    break\n",
    "                j += 1\n",
    "            res = max(res, j - i)\n",
    "            i = j    \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        t = []\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] <= threshold:\n",
    "                if nums[i] % 2 == 0:\n",
    "                    if len(t) == 0:\n",
    "                        t.append(nums[i])\n",
    "                    else:\n",
    "                        if t[-1] % 2 != 0:\n",
    "                            t.append(nums[i])\n",
    "                        else:\n",
    "                            ans = max(ans,len(t))\n",
    "                            t = [nums[i]]\n",
    "                else:\n",
    "                    if len(t) == 0:\n",
    "                        pass\n",
    "                    else:\n",
    "                        if t[-1] % 2 == 0:\n",
    "                            t.append(nums[i])\n",
    "                        else:\n",
    "                            ans = max(ans,len(t))\n",
    "                            t = []\n",
    "            else:\n",
    "                ans = max(ans,len(t))\n",
    "                t = []\n",
    "            # print(t)\n",
    "        return max(ans,len(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\r\n",
    "        # - 预处理数组\r\n",
    "        new_n = [num & 1 if num <= threshold else -1 for num in nums]\r\n",
    "        ans, cnt = 0, 0\r\n",
    "        for i in range(len(new_n)):\r\n",
    "            if cnt == 0:\r\n",
    "                if new_n[i] != 0:\r\n",
    "                    continue\r\n",
    "                else:\r\n",
    "                    cnt += 1\r\n",
    "            elif new_n[i] == -1:\r\n",
    "                ans = max(ans, cnt)\r\n",
    "                cnt = 0\r\n",
    "            elif new_n[i] != new_n[i-1]:\r\n",
    "                cnt += 1\r\n",
    "            elif new_n[i] == 0:\r\n",
    "                ans = max(ans, cnt)\r\n",
    "                cnt = 1\r\n",
    "            elif new_n[i] == 1:\r\n",
    "                ans = max(ans, cnt)\r\n",
    "                cnt = 0\r\n",
    "        ans = max(ans, cnt)\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l = len(nums)     \n",
    "        ans, cur_ans = 0, 0\n",
    "        for i in range(l - 1):\n",
    "            if cur_ans == 0 and nums[i] % 2 != 0:\n",
    "                continue\n",
    "            if nums[i] > threshold or nums[i] % 2 == nums[i + 1] % 2:\n",
    "                if nums[i] <= threshold:\n",
    "                    cur_ans += 1\n",
    "                ans = max(ans, cur_ans)\n",
    "                cur_ans = 0\n",
    "                continue\n",
    "            cur_ans += 1\n",
    "\n",
    "        if ans == 0 and cur_ans == 0:\n",
    "            return 1 if nums[l - 1] % 2 == 0 and nums[l - 1] <= threshold else 0\n",
    "\n",
    "        if nums[l - 1] <= threshold:\n",
    "            cur_ans += 1\n",
    "\n",
    "        return max(ans, cur_ans)\n",
    "                \n",
    "            \n",
    "                \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: list[int], threshold: int) -> int:\n",
    "        max_num = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] % 2 != 0:\n",
    "                continue\n",
    "            if nums[i] > threshold:\n",
    "                continue\n",
    "            K = 1\n",
    "            for j in range(i+1, len(nums)):\n",
    "                if nums[j-1] % 2 == nums[j] % 2 or nums[j] > threshold:\n",
    "                    break\n",
    "                K += 1\n",
    "            max_num = max(max_num, K)\n",
    "        return max_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSatisfied(self, nums: List[int], l: int, r: int, threshold: int) -> bool:\n",
    "        if nums[l] % 2 != 0:\n",
    "            return False\n",
    "        for i in range(l, r + 1):\n",
    "            if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if self.isSatisfied(nums, l, r, threshold):\n",
    "                    res = max(res, r - l + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ret = 0\n",
    "        # for idx, val in enumerate(nums):\n",
    "        idx = 0\n",
    "        while idx < len(nums):\n",
    "            if nums[idx] <= threshold and nums[idx] % 2 == 0:\n",
    "                idx2 = idx + 1\n",
    "                while idx2 < len(nums) and nums[idx2] <= threshold:\n",
    "                    if nums[idx2] % 2 != nums[idx2 - 1] % 2:\n",
    "                        idx2 += 1\n",
    "                    else:\n",
    "                        break\n",
    "                ret = max(ret, idx2 - idx)\n",
    "                idx = idx2\n",
    "            else:\n",
    "                idx += 1\n",
    "        \n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSatisfied(self, nums: List[int], l: int, r: int, threshold: int) -> bool:\n",
    "        if nums[l] % 2 != 0:\n",
    "            return False\n",
    "        for i in range(l, r + 1):\n",
    "            if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if self.isSatisfied(nums, l, r, threshold):\n",
    "                    res = max(res, r - l + 1)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n, ans, i = len(nums), 0, 0\n",
    "        while i < n:\n",
    "            if nums[i] % 2 != 0 or nums[i] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            j, cur = i + 1, nums[i] % 2\n",
    "            while j < n:\n",
    "                if nums[j] > threshold or nums[j] % 2 == cur: break\n",
    "                cur, j = nums[j] % 2, j + 1\n",
    "            ans = max(ans, j - i)\n",
    "            i = j\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, dp = 0, 0\n",
    "        for l in range(len(nums) - 1, -1, -1):\n",
    "            if nums[l] > threshold:\n",
    "                dp = 0\n",
    "            elif l == len(nums) - 1 or nums[l] % 2 != nums[l + 1] % 2:\n",
    "                dp = dp + 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            res = dp if nums[l] % 2 == 0 and dp > res else res\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        if nums[0] <= threshold and nums[0] % 2 == 0:\n",
    "            flag = True\n",
    "            res = cur_length = 1\n",
    "        else:\n",
    "            flag = False\n",
    "            res = cur_length = 0\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] <= threshold:\n",
    "                if not flag and nums[i] % 2 == 0:\n",
    "                    flag = True\n",
    "                    cur_length = 1\n",
    "                elif flag and (nums[i] + nums[i - 1]) % 2 == 1:\n",
    "                    cur_length += 1\n",
    "                elif nums[i] % 2 == 0:\n",
    "                    flag = True\n",
    "                    cur_length = 1\n",
    "                else:\n",
    "                    flag = False\n",
    "                    cur_length = 0\n",
    "            else:\n",
    "                flag = False\n",
    "                cur_length = 0\n",
    "            res = max(res, cur_length)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        l = r = 0\n",
    "        while r < n:\n",
    "            if nums[r] <= threshold and nums[r] % 2 == 0:\n",
    "                r += 1\n",
    "                while r < n and nums[r] <= threshold and nums[r] % 2 != nums[r - 1] % 2:\n",
    "                    r += 1\n",
    "                ans = max(ans, r - l)\n",
    "                l = r\n",
    "            else:\n",
    "                l += 1\n",
    "                r += 1\n",
    "            \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        self.threshold = threshold\n",
    "        max_answer = 0  # 最大答案\n",
    "\n",
    "        l = 0   # 开头\n",
    "        r = len(nums)   # 结尾\n",
    "        while l < r:\n",
    "            answer = self._check(nums[l:])\n",
    "            max_answer = max(answer, max_answer)\n",
    "            if answer == 0:\n",
    "                l += 1\n",
    "            l += answer\n",
    "\n",
    "        return max_answer\n",
    "\n",
    "    def _check(self, nums: List[int]) -> int:\n",
    "        odd = False\n",
    "        cut = 0\n",
    "        for n in nums:\n",
    "            if n > self.threshold:\n",
    "                break\n",
    "            if n % 2 == odd:\n",
    "                cut += 1\n",
    "                odd = not odd\n",
    "            else:\n",
    "                break\n",
    "        return cut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "    res = 0\n",
    "    nl = len(nums)\n",
    "    x = 1\n",
    "    i = 0\n",
    "    while i < nl:\n",
    "      if nums[i] % 2 == 0 and nums[i] <= threshold:\n",
    "        x = 1\n",
    "        while i + 1 < nl and nums[i+1] <= threshold and (nums[i] + nums[i+1]) % 2 == 1:\n",
    "          # print(nums[i], nums[i+1], (nums[i] ^ nums[i+1]) & 1)\n",
    "          x += 1\n",
    "          i += 1\n",
    "        res = max(res, x)\n",
    "      i += 1\n",
    "    return res\n",
    "      \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l = -1\n",
    "        ans = 0\n",
    "        for i in range(len(nums)):\n",
    "            if l == -1 and nums[i] % 2 == 0 and nums[i] <= threshold:\n",
    "                l = i\n",
    "            else:\n",
    "                if l != -1:\n",
    "                    if nums[i] > threshold:\n",
    "                        ans = max(ans,i-l)\n",
    "                        l = -1\n",
    "                    elif nums[i-1]%2 == nums[i]%2:\n",
    "                        ans = max(ans,i-l)\n",
    "                        l = -1\n",
    "                        if nums[i] % 2 == 0:\n",
    "                            l = i\n",
    "                    else:\n",
    "                        continue\n",
    "        if l != -1:\n",
    "            ans = max(ans,len(nums)-l)\n",
    "        \n",
    "        return ans\n",
    "            \n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n, ans, i = len(nums), 0, 0\n",
    "        while i < n:\n",
    "            if nums[i]%2 != 0 or nums[i] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            j, cur = i+1, nums[i]%2\n",
    "            while j < n:\n",
    "                if nums[j] > threshold or nums[j] % 2 == cur:\n",
    "                    break\n",
    "                cur, j = nums[j] % 2, j + 1\n",
    "            ans = max(ans, j-i)\n",
    "            i = j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSatisfied(self, nums: List[int], l: int, r: int, threshold: int) -> bool:\n",
    "        if nums[l] % 2 != 0:\n",
    "            return False\n",
    "        for i in range(l, r + 1):\n",
    "            if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if self.isSatisfied(nums, l, r, threshold):\n",
    "                    res = max(res, r - l + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l = 0\n",
    "        ans = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if num > threshold:\n",
    "                l = i + 1\n",
    "            elif l == i:\n",
    "                if num & 1:\n",
    "                    l = i + 1\n",
    "            else:\n",
    "                if (num ^ nums[i-1]) & 1 == 0:\n",
    "                    l =  i + 1 if num & 1 else i\n",
    "            ans = max(ans, i + 1 - l)\n",
    "        return ans\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        LENGTH = len(nums)\n",
    "        ans = 0\n",
    "        evens, odds = 0, 0\n",
    "        flag = -1\n",
    "        #flag == 1 means odds\n",
    "        #flag == 0 means evens\n",
    "        #flag == -1 means new start\n",
    "        for i in range(LENGTH):\n",
    "            if nums[i] > threshold:\n",
    "                evens, odds = 0, 0\n",
    "                flag = -1 # start a new count\n",
    "                continue\n",
    "            if nums[i] % 2 == 0:\n",
    "                if flag == 0:\n",
    "                    # flag = 0 no need to assign again\n",
    "                    evens = 1\n",
    "                    odds = 0\n",
    "                else:                   \n",
    "                    flag = 0\n",
    "                    evens += 1\n",
    "                    ans = max(ans, odds + evens)\n",
    "            else:\n",
    "                if flag == 0:\n",
    "                    flag = 1\n",
    "                    odds += 1\n",
    "                    ans = max(ans, odds + evens)\n",
    "                else:\n",
    "                    flag = 1  # this is necessary, incase flag == -1\n",
    "                    evens = 0\n",
    "                    odds = 0\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, dp = 0, 0\n",
    "        for l in range(len(nums) - 1, -1, -1):\n",
    "            if nums[l] > threshold:\n",
    "                dp = 0\n",
    "            elif l == len(nums) - 1 or nums[l] % 2 != nums[l + 1] % 2:\n",
    "                dp = dp + 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            res = dp if nums[l] % 2 == 0 and dp > res else res\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSatisfied(self, nums: List[int], l: int, r: int, threshold: int) -> bool:\n",
    "        if nums[l] % 2 != 0:\n",
    "            return False\n",
    "        for i in range(l, r + 1):\n",
    "            if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if self.isSatisfied(nums, l, r, threshold):\n",
    "                    res = max(res, r - l + 1)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            res = max(res, i - start)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l = len(nums)\n",
    "        ans = 0\n",
    "        cur = 0\n",
    "        i = j = 0\n",
    "        while i < l:\n",
    "            if nums[i] % 2 == 0 and nums[i] <= threshold:\n",
    "                cur = 1\n",
    "                j = i+1\n",
    "                while j < l and (nums[j]%2) != (nums[j-1]%2) and nums[j] <= threshold:\n",
    "                    j += 1\n",
    "                    cur += 1\n",
    "                ans = max(ans, cur)\n",
    "                i = j\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: list[int], threshold: int) -> int:\n",
    "        L = len(nums)\n",
    "        subarray_length = 0\n",
    "        for i in range(0, L):\n",
    "            for j in range(i, L):\n",
    "                if self.check_conditions(nums[i:j+1], threshold) == True:\n",
    "                    subarray_length = max(subarray_length, j+1-i)\n",
    "        return subarray_length\n",
    "                \n",
    "    def check_conditions(self, subarray: list[int], threshold: int) -> bool:\n",
    "        if subarray[0] % 2 == 1: return False\n",
    "        L = len(subarray)\n",
    "        for i in range(0, L):\n",
    "            if subarray[i] % 2 != i % 2: return False\n",
    "            if not subarray[i] <= threshold: return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: list[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        i = 0  # 初始标记\n",
    "        j = 0\n",
    "        n = len(nums)\n",
    "        while i < n:\n",
    "            if nums[i] % 2 == 1 or nums[i] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                j += 1\n",
    "                i += 1\n",
    "                while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                    j += 1\n",
    "                    i += 1\n",
    "                ans = max(ans,j)\n",
    "                j = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\r\n",
    "        maxLen = 0\r\n",
    "        l = 0\r\n",
    "        while l < len(nums):\r\n",
    "            if nums[l] % 2 != 0 or nums[l] > threshold:\r\n",
    "                l += 1\r\n",
    "                continue\r\n",
    "            j = l+1\r\n",
    "            while j < len(nums):\r\n",
    "                if nums[j]%2 != nums[j-1]%2 and nums[j] <= threshold:\r\n",
    "                    j += 1\r\n",
    "                else:\r\n",
    "                    break\r\n",
    "            if j-l > maxLen:\r\n",
    "                maxLen = j-l\r\n",
    "            l = j\r\n",
    "        return maxLen\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            if nums[i] % 2 != 0 or nums[i] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            j = i + 1\n",
    "            while j < n:\n",
    "                if nums[j] % 2 == nums[j-1] % 2 or nums[j] > threshold:\n",
    "                    break\n",
    "                j += 1\n",
    "            ans = max(ans,j-i)\n",
    "            i = j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            if nums[i]%2!=0 or nums[i]>threshold:\n",
    "                i+=1\n",
    "                continue\n",
    "            tempans=1\n",
    "            i+=1\n",
    "            while i<n and nums[i-1]%2!=nums[i]%2 and nums[i]<=threshold:\n",
    "                i+=1\n",
    "                tempans+=1\n",
    "            ans=max(ans,tempans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i =0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i<n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] %2:\n",
    "                i += 1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, dp = 0, 0\n",
    "        for l in range(len(nums) - 1, -1, -1):\n",
    "            if nums[l] > threshold:\n",
    "                dp = 0\n",
    "            elif l == len(nums) - 1 or nums[l] % 2 != nums[l + 1] % 2:\n",
    "                dp = dp + 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            res = dp if nums[l] % 2 == 0 and dp > res else res\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的（并且无法再延长的）子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 时间复杂度O(n)\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i \n",
    "            i += 1 # 从下一个位置判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        max_l = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] % 2 != 0 or nums[i] > threshold:\n",
    "                continue\n",
    "            l = i + 1\n",
    "            sub_l = 1\n",
    "            while l < len(nums):\n",
    "                if nums[l] > threshold:\n",
    "                    break\n",
    "                if nums[l] % 2 != nums[l - 1] % 2:\n",
    "                    sub_l += 1\n",
    "                else:\n",
    "                    break\n",
    "                l += 1\n",
    "            max_l = max(max_l, sub_l)\n",
    "        return max_l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        # n = len(nums)\n",
    "        # max_len = 0\n",
    "        # for l in range(n):\n",
    "        #     for r in range(l+max_len, n):\n",
    "        #         flag1 = nums[l] % 2 == 0\n",
    "        #         flag2 = all([nums[l + k] % 2 == k % 2 for k in range(r-l+1)]) # k: 0, ..., r-l\n",
    "        #         flag3 = all([nums[i] <= threshold for i in range(l, r+1)])\n",
    "        #         # print(l, r, flag1, flag2, flag3)\n",
    "        #         if flag1 and flag2 and flag3:\n",
    "        #             new_len = r - l + 1\n",
    "        #             if new_len > max_len:\n",
    "        #                 max_len = new_len\n",
    "        # return max_len\n",
    "\n",
    "\n",
    "\n",
    "        # n = len(nums)\n",
    "        # flag1 = [nums[k] % 2 == 0 for k in range(n)]\n",
    "        # flag2 = [nums[k] % 2 != nums[k+1] % 2 for k in range(n-1)] + [True]\n",
    "        # flag3 = [nums[k] <= threshold for k in range(n)]\n",
    "\n",
    "        # tmp = False\n",
    "        # max_len = 0\n",
    "        # for i in range(n): \n",
    "        #     if flag1[i] and tmp == False:\n",
    "        #         tmp = True\n",
    "        #         len_tmp = 0\n",
    "        #     ## no need to consider because shorter\n",
    "        #     # elif flag1[i] and tmp:\n",
    "        #     #     pass\n",
    "        #     if tmp:\n",
    "        #         if flag2[i] and flag3[i]:\n",
    "        #             len_tmp += 1\n",
    "        #         elif flag3[i]:\n",
    "        #             tmp = False\n",
    "        #             len_tmp += 1\n",
    "        #         else:\n",
    "        #             tmp = False                    \n",
    "        #         if len_tmp > max_len:\n",
    "        #             max_len = len_tmp\n",
    "        # return max_len\n",
    "\n",
    "\n",
    "        dp = 0\n",
    "        ans = 0\n",
    "        for i in range(len(nums)-1, -1, -1): # start, stop, step\n",
    "            if nums[i] > threshold:\n",
    "                dp = 0\n",
    "            elif i == len(nums)-1 or nums[i] % 2 != nums[i + 1] % 2:\n",
    "                dp += 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            ans = dp if nums[i] % 2 == 0 and dp > ans else ans\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        \n",
    "        n = len(nums)\n",
    "        i, res, j = 0, 0, 0\n",
    "\n",
    "        while i < n:\n",
    "            j = 0\n",
    "            while i < n and (nums[i] % 2 == 1 or (nums[i] % 2 == 0 and nums[i] > threshold)):\n",
    "                i += 1\n",
    "            while i + 1 < n and nums[i] % 2 != nums[i + 1] % 2 and nums[i] <= threshold and nums[i + 1] <= threshold:\n",
    "                i += 1\n",
    "                j += 1\n",
    "            if i < n and nums[i] <= threshold:\n",
    "                j += 1\n",
    "            res = max(res, j)\n",
    "            i += 1\n",
    "        return res\n",
    "\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        #外层循环负责记录开始位置和更新答案最大值\n",
    "        #内层循环负责遍历外层循环划分的组，找出这一组最远在哪结束\n",
    "        n = len(nums)\n",
    "        ans = i = 0 #记录着当前最大长度\n",
    "        #外层循环开始寻找开始位置\n",
    "        while i < n:\n",
    "            #如果当前位置满足这两个条件中的任意一个，则当前位置不可用，直接跳过\n",
    "            #如果当前位置不满足这两个条件中的任意一个，则if=False，直接跳过，说明当前位置可用（可作为起始位置）\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1 #直接跳过当前位置\n",
    "                continue\n",
    "            start = i #将这一组当前位置的下标用start记录下来，是这一组起始位置\n",
    "            i += 1 #从起始位置的下一个元素开始遍历\n",
    "\n",
    "            #满足这三个条件，则一直往后遍历nums，直至跳出循环，跳出循环的位置就是最终位置\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans, i - start) #将ans的值与当前循环得出的子数组长度比较，取出最大值存在变量ans中\n",
    "        return ans\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        i = ans = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1\n",
    "                continue\n",
    "            else:\n",
    "                start = i\n",
    "                i += 1\n",
    "                while i < n and nums[i]% 2 != nums[i-1]%2 and nums[i]<=threshold:\n",
    "                    i += 1\n",
    "                ans = max(ans,i-start)\n",
    "        return ans \n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l, r, ans = 0, 0, 0\n",
    "        while r < len(nums):\n",
    "            if (nums[l] & 1) == 1 or nums[l] > threshold:\n",
    "                l += 1\n",
    "                if l > r:\n",
    "                    r = l\n",
    "            else:\n",
    "                f = 0\n",
    "                while r < len(nums):\n",
    "                    if nums[r] > threshold or (nums[r] & 1) != f:\n",
    "                        break\n",
    "                    f ^= 1\n",
    "                    r += 1\n",
    "                if r - l > ans:\n",
    "                    ans = r - l\n",
    "                l = r\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res, dp = 0, 0\n",
    "        for l in range(len(nums) - 1, -1, -1):\n",
    "            if nums[l] > threshold:\n",
    "                dp = 0\n",
    "            elif l == len(nums) - 1 or nums[l] % 2 != nums[l + 1] % 2:\n",
    "                dp = dp + 1\n",
    "            else:\n",
    "                dp = 1\n",
    "            res = dp if nums[l] % 2 == 0 and dp > res else res\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        res = 0\n",
    "        for i, v in enumerate(nums):\n",
    "            if v <= threshold and v % 2  == 0:\n",
    "                t_res = 1\n",
    "                for j in range(i+1, n):\n",
    "                    if nums[j] <= threshold and nums[j] % 2 != nums[j-1] % 2:\n",
    "                        t_res += 1\n",
    "                    else:\n",
    "                        break\n",
    "                res = max(res, t_res)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        i = 0 \n",
    "        ans = 0 \n",
    "        n = len(nums)\n",
    "        while i < n:\n",
    "            if nums[i]%2==0 and nums[i]<=threshold:\n",
    "                st = i\n",
    "                i+=1\n",
    "                while i<n and nums[i]<=threshold and nums[i]%2 !=nums[i-1]%2:\n",
    "                    i+=1\n",
    "                ans = max(ans,i-st)\n",
    "\n",
    "            else:\n",
    "                i+=1 \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        c = r = 0\n",
    "        for v in nums:\n",
    "            if v > threshold:\n",
    "                c = 0\n",
    "                continue\n",
    "            elif v & 1 != c & 1:\n",
    "                c = v & 1 ^ 1\n",
    "            else:\n",
    "                c += 1\n",
    "            r = max(r, c)\n",
    "        return max(r, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 时间复杂度O(n)\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and nums[i] % 2 != nums[i-1] % 2 and nums[i] <= threshold:\n",
    "                i += 1\n",
    "            ans = max(ans,i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2 != 0:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i  \n",
    "            i += 1 # 下一个位置判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "            ans = max(ans,i - start)\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        result=0\n",
    "        left=-1\n",
    "        for i in range(len(nums)):\n",
    "            n=nums[i]\n",
    "            if n>threshold:\n",
    "                if left>=0 and i-left>result:\n",
    "                    result=i-left\n",
    "                left=-1\n",
    "            else:\n",
    "                if left>=0:\n",
    "                    if n%2==nums[i-1]%2:\n",
    "                        if left>=0 and i-left>result:\n",
    "                            result=i-left\n",
    "                        if n%2==0:\n",
    "                            left=i\n",
    "                        else:\n",
    "                            left=-1\n",
    "                else:\n",
    "                    if n%2==0:\n",
    "                        left=i\n",
    "        if left>=0 and len(nums)-left>result:\n",
    "            result=len(nums)-left\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if nums[i]%2 == 0 and nums[i] <= threshold:\n",
    "                a = nums[i]%2\n",
    "                cnt = 1\n",
    "                for j in range(i+1,n):\n",
    "                    b = nums[j]%2\n",
    "                    if b != a and nums[j] <= threshold:\n",
    "                        a = b\n",
    "                        cnt += 1\n",
    "                    else:\n",
    "                        break\n",
    "                ans = max(ans,cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans,i = 0,0\n",
    "        while i<len(nums):\n",
    "            if nums[i]%2!=0 or nums[i]>threshold:\n",
    "                i+=1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i<len(nums) and nums[i]<=threshold and nums[i]%2!=nums[i-1]%2:\n",
    "                i+=1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        l, r, ans = 0, 0, 0\n",
    "        while r < len(nums):\n",
    "            if (nums[l] & 1) == 1 or nums[l] > threshold:\n",
    "                l += 1\n",
    "                r = max(l, r)\n",
    "            else:\n",
    "                f = 0\n",
    "                while r < len(nums):\n",
    "                    if nums[r] > threshold or (nums[r] & 1) != f:\n",
    "                        break\n",
    "                    f ^= 1\n",
    "                    r += 1\n",
    "                ans = max(r - l, ans)\n",
    "                l = r\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSatisfied(self, nums: List[int], l: int, r: int, threshold: int) -> bool:\n",
    "        if nums[l] % 2 != 0:\n",
    "            return False\n",
    "        for i in range(l, r + 1):\n",
    "            if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if self.isSatisfied(nums, l, r, threshold):\n",
    "                    res = max(res, r - l + 1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans=0\n",
    "        i=0\n",
    "        while i<len(nums):\n",
    "            if nums[i]%2!=0 or nums[i]>threshold:\n",
    "                i+=1\n",
    "                continue\n",
    "            j=i+1\n",
    "            val=nums[i]%2\n",
    "            while j<len(nums):\n",
    "                if nums[j]>threshold or nums[j]%2==val:\n",
    "                    break\n",
    "                val=nums[j]%2\n",
    "                j+=1\n",
    "            ans=max(ans,j-i)\n",
    "            i=j\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的（并且无法再延长的）子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "\n",
    "        while i < n:\n",
    "            left = i\n",
    "            if nums[left] % 2 != 0 or nums[left] > threshold:\n",
    "                i += 1\n",
    "                continue\n",
    "            i += 1\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i-1] % 2:\n",
    "                i += 1\n",
    "\n",
    "            ans = max(ans, i - left)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        length = len(nums)\n",
    "        i = 0\n",
    "        res = 0\n",
    "        while i <= length-1:\n",
    "            if not (nums[i]%2==0 and nums[i]<=threshold):\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i <= length-1 and nums[i] <= threshold and nums[i]%2 != nums[i-1]%2:\n",
    "                i += 1\n",
    "            res = max(res, i-1-start+1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def is_valid(nums, l, r, threshold):\n",
    "    if nums[l] % 2 != 0:\n",
    "        return False\n",
    "    for i in range(l, r + 1):\n",
    "        if nums[i] > threshold or (i < r and nums[i] % 2 == nums[i + 1] % 2):\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ans = 0\n",
    "        for l in range(0, len(nums)):\n",
    "            for r in range(l, len(nums)):\n",
    "                if is_valid(nums, l, r, threshold):\n",
    "                    ans = max(ans, r - l + 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        res = 0\n",
    "        l, r = 0, 0\n",
    "        while l < len(nums):\n",
    "            if nums[l] % 2 != 0:\n",
    "                l += 1\n",
    "                continue\n",
    "            if nums[l] > threshold:\n",
    "                l += 1\n",
    "                continue\n",
    "            r = l+1\n",
    "            while r < len(nums):\n",
    "                if nums[r] > threshold:\n",
    "                    break\n",
    "                if nums[r] % 2 == nums[r-1] % 2:\n",
    "                    break\n",
    "                r += 1\n",
    "            res = max(res, r-l)\n",
    "            l += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        ## 子数组的要求：1.偶数开头；2.不能连续奇数/偶数；3.所有数都不大于阈值\n",
    "        ## dp数组的定义：dp[i]代表以i结尾的子数组，最大的长度\n",
    "        dp = [0]*len(nums)\n",
    "        if nums[0]%2==0 and nums[0]<=threshold: dp[0]=1\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]>threshold:\n",
    "                dp[i]=0\n",
    "                continue\n",
    "            if (dp[i-1]>0 and nums[i-1]%2==1 and nums[i]%2==0) or (dp[i-1]>0 and  nums[i-1]%2==0 and nums[i]%2==1): \n",
    "                dp[i]=dp[i-1]+1\n",
    "                continue\n",
    "            if nums[i]%2==0:dp[i]=1\n",
    "            else: dp[i]=0\n",
    "\n",
    "        print(dp)\n",
    "        return max(dp)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i<n:\n",
    "            if nums[i]>threshold or nums[i]%2:\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i<n and nums[i]<=threshold and nums[i]%2!=nums[i-1]%2:\n",
    "                i += 1\n",
    "            ans = max(ans,i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的（并且无法再延长的）子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n=len(nums)\n",
    "        i=0\n",
    "        ans=0\n",
    "        while i<n:\n",
    "            if nums[i]%2!=0 or nums[i]>threshold:\n",
    "                i+=1\n",
    "                continue\n",
    "            tempans=i\n",
    "            i+=1\n",
    "            while i<n and nums[i-1]%2!=nums[i]%2 and nums[i]<=threshold:\n",
    "                i+=1\n",
    "            ans=max(ans,i-tempans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        n = len(nums)\n",
    "        ans = i = 0\n",
    "        while i < n:\n",
    "            if nums[i] > threshold or nums[i] % 2:\n",
    "                i += 1  # 直接跳过\n",
    "                continue\n",
    "            start = i  # 记录这一组的开始位置\n",
    "            i += 1  # 开始位置已经满足要求，从下一个位置开始判断\n",
    "            while i < n and nums[i] <= threshold and nums[i] % 2 != nums[i - 1] % 2:\n",
    "                i += 1\n",
    "            # 从 start 到 i-1 是满足题目要求的（并且无法再延长的）子数组\n",
    "            ans = max(ans, i - start)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:\n",
    "        i = 0\n",
    "        start = 0\n",
    "        res = 0\n",
    "        length = len(nums)\n",
    "        while i <= length-1:\n",
    "            if not (nums[i]%2==0 and nums[i]<=threshold):\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i <= length-1 and nums[i]%2 != nums[i-1]%2 and nums[i] <= threshold:\n",
    "                i += 1\n",
    "            res = max(res, i-1-start+1)\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
